package com.tozed.android.launcher.simpleui;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class FastBitmapDrawable extends Drawable
{
	private static final String TAG = "FastBitmapDrawable";
	
	private int mAlpha = 255;
	private Bitmap mBitmap;
	private int mHeight;
	private final Paint mPaint = new Paint();
	private int mWidth;

	public FastBitmapDrawable(Bitmap bmp)
	{
		mBitmap = bmp;
		
		if (bmp != null){
			mWidth = mBitmap.getWidth();
			mHeight = mBitmap.getHeight();
		}else{
			mHeight = 0;
			mWidth = 0;
		}
	}

	public void draw(Canvas canvas)
	{
		if (mBitmap != null && !mBitmap.isRecycled()){
			Rect rect = getBounds();
			canvas.drawBitmap(this.mBitmap, null, rect, this.mPaint);
		}else{
			Log.w(TAG, "The mBitmap is null,it can not be drawn on the canvas");
		}
	}

	public int getAlpha(){
		return mAlpha;
	}

	public Bitmap getBitmap(){
		return mBitmap;
	}

	public int getIntrinsicHeight(){
		return mHeight;
	}

	public int getIntrinsicWidth(){
		return mWidth;
	}

	public int getMinimumHeight(){
		return mHeight;
	}

	public int getMinimumWidth(){
		return mWidth;
	}

	public int getOpacity(){
		return -3;
	}

	public void setAlpha(int alpha){
		mAlpha = alpha;
		mPaint.setAlpha(alpha);
	}

	public void setBitmap(Bitmap bmp)
	{
		mBitmap = bmp;
		if (bmp != null){
			mWidth = mBitmap.getWidth();
			mHeight = mBitmap.getHeight();
		}else{
			mHeight = 0;
			mWidth = 0;
		}
	}

	public void setColorFilter(ColorFilter colorFilter){
		mPaint.setColorFilter(colorFilter);
	}

	public void setFilterBitmap(boolean flag){
		mPaint.setFilterBitmap(flag);
	}
}
